import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/Layout'
import Cookies from "js-cookie";

const routes = [

  //  ==== 登录 ====
  { path: '/login', name: 'Login', component: ()=> import('@/views/admin/Login')},

  {
    path: '/',
    name: 'Layout',
    component: Layout,
    //redirect实现重定向 从Layout定向到home
    redirect:"userList",
    children:[
      //在children内部定义子路由


      //  ====主页路由====
      { path:'home', name:'Home', component:() => import("@/views/Home")},

      //====  admin相关内容  ====

      //====  People  人员管理====
      // {path:'addPeople',name:'AddPeople',component:() => import('@/views/people/AddPeople')},
      { path:'userList',name:'UserList',component:() => import('@/views/people/user/UserList')},
      { path: 'editUser', name: 'EditUser', component: () => import('@/views/people/user/EditUser')},
      { path: 'addUser', name: 'AddUser', component: () => import('@/views/people/user/AddUser')},
      { path:'staffList',name:'StaffList',component:() => import('@/views/people/staff/StaffList')},
      { path: 'editStaff', name: 'EditStaff', component: () => import('@/views/people/staff/EditStaff')},
      { path: 'addStaff', name: 'AddStaff', component: () => import('@/views/people/staff/AddStaff')},
      { path:'adminList',name:'AdminList',component:() => import('@/views/admin/AdminList')},
      { path: 'editAdmin', name: 'EditAdmin', component: () => import('@/views/admin/EditAdmin')},
      { path: 'addAdmin', name: 'AddAdmin', component: () => import('@/views/admin/AddAdmin')},
      //====  Website  站点管理====


      //====  collections  藏品管理====


      //====  Mall  商城管理====


    ]
  },
  // { path:'*', component:() => import('@/views/404')}
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  // let token = to.query.token;
  // if (token){
  // 	$.db.set("token",token,120);
  // }
  next();
})

//路由守卫,如果当前路径不是login,强制跳转到login
// router.beforeEach((to,from,next)  => {
//   if(to.path === '/login') next()
//   const admin = Cookies.get("admin")
//   if(!admin && to.path !== '/login')
//     return next("/login")
//   //访问 /home的时候,且cookie里存放数据,此时直接放行
//   next()
// })

router.afterEach((to, from, next) => {
  let title = "博物馆后台管理系统";
  document.title = title;
})



export default router
